<?php

class MyPDO
{
    private $dbname = "test0925";
    private $host = "127.0.0.1";
    private $user = "root";
    private $pass = "";
    private $charts = "utf8";
    //成员变量 $db
    private $db = null;//存储连接数据库后的结果

    //在对象被实例化时进行默认操作
    function __construct()
    {
        try{
        //连接数据库
        $this->db = new PDO("mysql:dbname=$this->dbname;host=$this->host","$this->user","$this->pass");
        $this->db->exec("set names $this->charts");
        //判断是否连接成功
        }catch (Exception $e){
            var_dump($e);
            die("连接失败");
        }
    }

    //增
    function insert($sql){
       return $this->db->exec($sql);
    }
    //删
    function delete($sql){
        return $this->db->exec($sql);
    }
    //改
    function update($sql){
        return $this->db->exec($sql);
    }
    //查
    function select($sql){
        //查询拿到的结果是一个PDO数据集合
        $pdoObject = $this->db->query($sql);
        //转成大家认识的关联数组
        $arr = [];
        foreach ($pdoObject as $item){
            array_push($arr,$item);
        }
        return $arr;
    }

}